<?php

namespace workin\modules\increment\controllers;

use common\components\increment\Config;
use common\components\increment\Huirong;
use common\components\tools\Tools;
use common\models\workin\CarInvonceVerifyHuirong;
use common\models\workin\IdcardCheck;
use common\models\workin\MortgageSignupHuirong;
use workin\modules\increment\models\CarInvonceVerifyHuirongSearch;
use workin\modules\increment\models\IdcardCheckSearch;
use workin\modules\increment\models\MortgageSignupHuirongSearch;
use yii\base\ErrorException;
use yii\helpers\FileHelper;
use yii\web\Controller;
use yii\web\UploadedFile;

/**
 * Default controller for the `test` module
 */
class DefaultController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionCarInvonceVerifyList()
    {
        $searchModel = new CarInvonceVerifyHuirongSearch();
        $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);

        return $this->render('car-invonce-verify-list', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * 1.8购车发票验证
     */
    public function actionCarInvonceVerify()
    {
        $model = new CarInvonceVerifyHuirong();
        $model->grant_key = Config::$huirong['grantKey'];
        $model->api_key = Config::$huirong['car_invoice_verify_api_key'];
        $model->pic_type = 1101;
        $model->call_back = \Yii::$app->params['api_domain']."/other/hui-rong/car-invonce-verify-back";
        $model->order_report_id = \Yii::$app->security->generateRandomString(22).rand(10000000,99999999);
        if ($si = \Yii::$app->request->get('search_index'))$model->search_index = $si;
        if ($model->load(\Yii::$app->request->post())){
            $model->pic = UploadedFile::getInstance($model, 'pic');
            $base64 = base64_encode(file_get_contents($model->pic->tempName));
            if ($model->validate()){
                FileHelper::createDirectory(\Yii::getAlias('@uploads_root/huirong'));
                $model->pic->saveAs(\Yii::getAlias('@uploads_root/huirong')."/".time().rand(1000,9999)."_".$model->pic->baseName.'.'.$model->pic->extension);
                $huirong = new Huirong();
                $huirong->verifyModel = $model;
                $huirong->pic_base64 = $base64;
                $r = $huirong->carInvonceVerify();
                $_r = json_decode($r->content);
                if ($_r->code==0){
                    if ($huirong->verifyModel->save()){
                        \Yii::$app->session->setFlash('success', "保存成功");
                    }else{
                        \Yii::$app->session->setFlash('error', Tools::makeModelGetErrorsToStringAndGetIt($huirong->verifyModel));
                    }
                }else{
                    \Yii::$app->session->setFlash('error', $_r->msg."[".$r->content."]");
                }
            }
        }
        return $this->render('car-invonce-verify', [
            'model' => $model,
        ]);
    }

    public function actionGetCivBackResult()
    {
        if ($si = \Yii::$app->request->get('search_index')){}else{
            throw new ErrorException("没有get到search_index参数");
        }
        $model = CarInvonceVerifyHuirong::findOne(['search_index'=>$si]);
        return $this->render('civ-query-result.php', [
            'model' => $model,
        ]);
    }

    /**
     * 1.11抵押登记
     */
    public function actionMortgageSignup()
    {
        $model = new MortgageSignupHuirong();
        $model->grant_key = Config::$huirong['grantKey'];
        $model->api_key = Config::$huirong['mortgage_signup_api_key'];
        $model->call_back = \Yii::$app->params['api_domain']."/other/hui-rong/mortgage-signup-back";
        $model->order_report_id = \Yii::$app->security->generateRandomString(32).rand(10000000,99999999);
        if ($model->load(\Yii::$app->request->post())){
            if ($model->validate()){
                $huirong = new Huirong();
                $huirong->model1 = $model;
                $r = $huirong->mortgageSignup();
                $_r = json_decode($r->content);
                if ($_r->code==0){
                    if ($_r->data->orderReportId==$model->order_report_id){
                        $huirong->model1->report_id = $_r->data->reportId;
                        if ($huirong->model1->save()){
                            \Yii::$app->session->setFlash('success', "保存成功");
                        }else{
                            \Yii::$app->session->setFlash('error', Tools::makeModelGetErrorsToStringAndGetIt($huirong->model1));
                        }
                    }else{
                        \Yii::$app->session->setFlash('error', "结果的orderReportId与请求的order_report_id不符");
                    }
                }else{
                    \Yii::$app->session->setFlash('error', $_r->msg."[".$r->content."]");
                }
            }
        }
        return $this->render('mortgage-signup', [
            'model' => $model,
        ]);
    }

    /**
     * 1.08
     * @return string
     */
    public function actionGetMsBackResultList()
    {
        $searchModel = new MortgageSignupHuirongSearch();
        $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);

        return $this->render('ms-back-result-list', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * 1.11
     * @return string
     * @throws ErrorException
     */
    public function actionGetMsBackResult()
    {
        if ($si = \Yii::$app->request->get('search_index')){}else{
            throw new ErrorException("没有get到search_index参数");
        }
        $model = MortgageSignupHuirong::findOne(['search_index'=>$si]);
        return $this->render('ms-back-result.php', [
            'model' => $model,
        ]);
    }

    /**
     * 1.24身份证实名认证(带照)
     */
    public function actionIdCardCheck0()
    {
        $model = new IdcardCheck();
        $model->grant_key = Config::$huirong['grantKey'];
        $model->api_key = Config::$huirong['id_card_check0'];
        $model->order_report_id = \Yii::$app->security->generateRandomString(32).rand(10000000,99999999);
        if ($model->load(\Yii::$app->request->post())){
            if ($model->validate()){
                $huirong = new Huirong();
                $huirong->model2 = $model;
                $r = $huirong->idcardCheck0();
                $_r = json_decode($r->content);
                if ($_r->code==0){
                    if ($_r->data->orderReportId==$model->order_report_id){
                        $huirong->model2->result_detail = $r->content;
                        $huirong->model2->result_photo_base64 = $_r->data->photoBase64;
                        $huirong->model2->result_result = $_r->data->result;
                        $huirong->model2->result_report_id = $_r->data->reportId;
//                        $huirong->model2->result_auth_status = $_r->data->authStatus;
                        $huirong->model2->result_is_real = $_r->data->isReal;
                        $huirong->model2->result_service_fee = $_r->data->serviceFee;
                        $huirong->model2->result_exp_status = $_r->data->expStatus;
                        $huirong->model2->result_busi_code = $_r->data->busiCode;
                        $huirong->model2->result_busi_msg = $_r->data->busiMsg;
                        if (isset($_r->data->extendData))$huirong->model2->result_extend_data = $_r->data->extendData;
                        if ($huirong->model2->save()){
                            \Yii::$app->session->setFlash('success', "保存成功");
                        }else{
                            \Yii::$app->session->setFlash('error', Tools::makeModelGetErrorsToStringAndGetIt($huirong->model2));
                        }
                    }else{
                        \Yii::$app->session->setFlash('error', "结果的orderReportId与请求的order_report_id不符");
                    }
                }else{
                    \Yii::$app->session->setFlash('error', $_r->msg."[".$r->content."]");
                }
            }
        }
        return $this->render('idcard-check0', [
            'model' => $model,
        ]);
    }

    public function actionIdCardCheck0ResultList()
    {
        $searchModel = new IdcardCheckSearch();
        $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);

        return $this->render('idcard-check0-result-list', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
}
